package cn.chenyumeng.blog.controller.admin;

import cn.chenyumeng.blog.dto.BlogDTO;
import cn.chenyumeng.blog.dto.CommonResponse;
import cn.chenyumeng.blog.dto.ResultCode;
import cn.chenyumeng.blog.service.admin.BlogService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @program: blog->BlogController
 * @description:
 * @author: cg
 * @create: 2020-07-27 16:52
 **/
@Validated
@RestController
@ResponseBody
@RequestMapping("/admin/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @GetMapping("/queryBlogById")
    public CommonResponse queryBlogById(@NotNull(message = "博客Id不能为空") Long id) {
        BlogDTO blogDTO = blogService.queryById(id);
        if (blogDTO == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(blogDTO);
    }

    @GetMapping("/queryAllByLimit")
    public CommonResponse queryAllByLimit(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                          @RequestParam(name = "size", defaultValue = "10") Integer size) {
        PageInfo<BlogDTO> blogPageInfo = blogService.queryAllByLimit(page, size);
        if (blogPageInfo == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(blogPageInfo);
    }

    @PostMapping("/queryAllByBlogAndLimit")
    public CommonResponse queryAllByBlogAndLimit(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                 @RequestParam(name = "size", defaultValue = "10") Integer size,
                                                 BlogDTO dto) {
        PageInfo<BlogDTO> dtoPageInfo = blogService.queryAllByBlog(page, size, dto);
        if (dtoPageInfo == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(dtoPageInfo);
    }

    @PostMapping("/saveBlog")
    public CommonResponse saveBlog(@Validated BlogDTO dto) {
        BlogDTO insert = blogService.insert(dto);
        if (insert == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(insert);
    }

    @PostMapping("/updateBlog")
    public CommonResponse updateBlog(@Validated BlogDTO dto) {
        BlogDTO update = blogService.update(dto);
        if (update == null) {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
        return CommonResponse.ok(update);
    }

    @GetMapping("/deleteBlogById")
    public CommonResponse deleteBlogById(@NotNull(message = "博客id不能为空") Long id) {
        if (blogService.deleteById(id)) {
            return CommonResponse.ok();
        } else {
            return CommonResponse.build(ResultCode.PARAM_IS_INVALID);
        }
    }

}
